home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / shell / execute_upd.lha / Execute / adjust.c next >
Encoding:
C/C++ Source or Header  |  1995-02-28  |  3.1 KB  |  167 lines

  1. /*
  2.  * Routine to convert a UNIX-style path to an AmigaDOS path.
  3.  * A UNIX-style path is understood here as follows:
  4.  * 
  5.  *      leading . means current dir; like "", but filled in explicitly
  6.  *      ../ means /
  7.  *      ..  means /
  8.  * 
  9.  * Written as part of UnixDirs2, a (to be written) system patch which allows
  10.  * use of UNIX-style paths everywhere.
  11.  * 
  12.  * Martin W. Scott,  8 January 1993
  13.  */
  14. #include <exec/types.h>
  15. #include <exec/execbase.h>
  16. #include <dos/dos.h>
  17. #include <dos/dosextens.h>
  18. #include <clib/dos_protos.h>
  19. #include <clib/exec_protos.h>
  20. #include <pragmas/dos_lib.h>
  21. #include <pragmas/exec_lib.h>
  22.  
  23. #include <string.h>
  24. #include <stdio.h>
  25.  
  26. extern struct ExecBase *SysBase;
  27.  
  28. /* insert $cwd into s, return pointer to next free char */
  29. static char *
  30. insertcwd (char *s, LONG len)
  31. {
  32.   geta4 ();
  33.  
  34.   if (NameFromLock (((struct Process *) (SysBase->ThisTask))->pr_CurrentDir, s, len))
  35.     {
  36.       while (*s)
  37.     s++;
  38.     }
  39.   return s;
  40. }
  41.  
  42. /* adjust path from UNIX-style to Amiga-style */
  43. /* TO DO: length checking when building new path */
  44. static BOOL
  45. adjustpath (char *path, char *newpath, LONG len)
  46. {
  47.   char *s, *t;
  48.  
  49.   /*  geta4 (); */
  50.  
  51.   s = newpath;
  52.  
  53.   if (path == NULL)        /* bypass */
  54.     return FALSE;
  55.  
  56.   if (t = strchr (path, ':'))    /* check for ':' in path */
  57.     {
  58.       t++;            /* copy device component */
  59.       while (path < t)
  60.     *s++ = *path++;
  61.     }
  62.   else if (path[0] == '/')
  63.     {
  64.       path++;
  65.  
  66.       if (t = strchr (path, '/'))
  67.     {
  68.       while (path < t)
  69.         *s++ = *path++;
  70.  
  71.       *s++ = ':', path++;
  72.     }
  73.       else
  74.     {
  75.       while (*path)
  76.         *s++ = *path++;
  77.  
  78.       *s++ = ':';
  79.     }
  80.     }
  81.   else if (path[0] == '.')
  82.     {
  83. /*** translate '.' to $cwd ***/
  84.       if (!path[1])        /* only "." */
  85.     {
  86.       s = insertcwd (s, len);
  87.       path++;        /* path[0] == '\0' - STOP */
  88.     }
  89.       else if (path[1] == '/')    /* initial component is $cwd */
  90.     {
  91.       s = insertcwd (s, len);
  92.       if (*(s - 1) != ':')
  93.         *s++ = '/';        /* copy '/', increment pointers */
  94.       path += 2;
  95.     }
  96.     }
  97.  
  98.   while (path[0])
  99.     {
  100. /*** copy remainder of path ***/
  101.       if (path[0] == '.' && path[1] == '.')
  102.     {
  103.       if (path[2] == '/')    /* just skip "..", copying '/' */
  104.         {
  105.           path += 2;
  106.           *s++ = *path++;
  107.         }
  108.       else if (!path[2])    /* append '/' and stop */
  109.         {
  110.           path += 2;
  111.           *s++ = '/';
  112.         }
  113.       else
  114.         *s++ = *path++;
  115.     }
  116.       else
  117.     *s++ = *path++;
  118.     }
  119.   *s = '\0';
  120.  
  121.   return TRUE;
  122. }
  123.  
  124. int
  125. wedge ()
  126. {
  127.   int retval = 0;
  128.  
  129.   char *arg_str, *copy_of_arg_str;
  130.  
  131.   if (copy_of_arg_str = strdup (arg_str = GetArgStr ()))
  132.     {
  133.       char *script, *ptr = copy_of_arg_str;
  134.  
  135.       if (script = strsep (&ptr, " \t\n"))
  136.     {
  137.       FILE *fd;
  138.  
  139.       if (fd = fopen (script, "r"))
  140.         {
  141.           /* buffers should handle all but the most extreme of extreme cases */
  142.           char tmp_buf1[512] = "", tmp_buf2[512] = "";
  143.  
  144.           fgets (tmp_buf1, 512, fd);
  145.  
  146.           fclose (fd);
  147.  
  148.           if ((tmp_buf1[0] == '#') && (tmp_buf1[1] == '!'))
  149.         {
  150.           char *interpretor, *ptr = &tmp_buf1[2];
  151.  
  152.           if (interpretor = strsep (&ptr, " \t\n"))
  153.             {
  154.               adjustpath (tmp_buf1, tmp_buf2, 512);
  155.  
  156.               printf (tmp_buf1, "%s %s", tmp_buf2, arg_str); 
  157.  
  158.               retval = 1;
  159.             }
  160.         }
  161.         }
  162.     }
  163.       free (copy_of_arg_str);
  164.     }
  165.   return (retval);
  166. }
  167.